package com.uinnova.springboot.demo.web;

import com.uinnova.springboot.demo.web.filter.MyFilter2;
import com.uinnova.springboot.demo.web.interceptor.MyInterceptor1;
import com.uinnova.springboot.demo.web.interceptor.MyInterceptor2;
import com.uinnova.springboot.demo.web.listener.MyServletContextListener;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.web.servlet.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.servlet.config.annotation.*;

import javax.servlet.MultipartConfigElement;
import java.util.Arrays;
import java.util.List;

/**
 * @author 张攀钦
 * @date 2018-12-21-18:53
 */
@Configuration
/**
 * @author 张攀钦
 * @date 2018/12/21-19:17 
 * @ServletComponentScan 扫描当前包和当前包的子包 web filter listener
 */
@ServletComponentScan
public class WebConfig implements WebMvcConfigurer {
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/" ,"classpath:/my"};

    /**
     * Add handlers to serve static resources such as images, js, and, css
     * files from specific locations under web application root, the classpath,
     * and others.
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
    }

    /**
     * Configure simple automated controllers pre-configured with the response
     * status code and/or a view to render the response body. This is useful in
     * cases where there is no need for custom controller logic -- e.g. render a
     * home page, perform simple site URL redirects, return a 404 status with
     * HTML content, a 204 with no content, and more.
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 配置路径重定向
        registry.addRedirectViewController("/","/a.png");
        // 类似转发
        registry.addViewController("/view").setViewName("/view.html");
    }

    /**
     * @author 张攀钦
     * @date 2018/12/21-23:05
     * 注册监听器
     */
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean() {
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
        servletListenerRegistrationBean.setListener(new MyServletContextListener());
        return servletListenerRegistrationBean;
    }

    /**
     * @author 张攀钦
     * @date 2018/12/21-23:03
     * 注册过滤器 /*
     */
    @Bean
    public FilterRegistrationBean filterRegistration() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new MyFilter2());
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

    /**
     * @author 张攀钦
     * @date 2018/12/21-23:04
     * 注册 web /*
     */

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        // ServletName默认值为首字母小写，即myServlet
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(new MyServlet());
        servletRegistrationBean.addUrlMappings("/web/*", "*.do");
        return servletRegistrationBean;
    }

    /**
     * @author 张攀钦
     * @date 2018/12/21-23:04
     * 添加拦截器 /**
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor1()).addPathPatterns("/**");
        registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**");
    }

    /**
     * @author 张攀钦
     * @date 2018/12/21-23:27
     * 文件上传配置
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 设置文件大小限制 ,超出设置页面会抛出异常信息，
        factory.setMaxFileSize(DataSize.of(512L, DataUnit.MEGABYTES));
        /// 设置总上传数据总大小
        factory.setMaxFileSize(DataSize.of(512L, DataUnit.KILOBYTES));
        return factory.createMultipartConfig();
    }
}
